﻿package core.ui.controls
{
    import core.enum.*;
    import core.event.*;
    import core.image.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;

    public class LayerInfo extends Sprite
    {
        public var nr:int;
        public var layer:Layer;
        private var ed:Tuyitu;
        private var nw:int;
        private var nh:int;
        private var matrix:Matrix;
        private var cb:Checkbox;
        private var ln:Literal;
        private var layerstyle:IconButton;
        private var locked:IconButton;
        private var thumb:Bitmap;
        private var masking:Bitmap;
        private var bg:Sprite;
        private var thumbholder:Sprite;
        private var maskholder:Sprite;
        private var markselected:Sprite;

        public function LayerInfo(param1:Tuyitu, param2:int, param3:Layer, param4:int, param5:int, param6:int)
        {
            this.ed = param1;
            this.nr = param2;
            this.layer = param3;
            var _loc_7:* = new Matrix();
            _loc_7.createGradientBox(param6, 39, 90 / (180 / Math.PI), 0, 0);
            this.bg = new Sprite();
            this.bg.graphics.beginGradientFill("linear", [6273533, 34534], [1, 1], [0, 255], _loc_7);
            this.bg.graphics.drawRect(0, 0, param6, 39);
            this.bg.graphics.endFill();
            this.bg.graphics.beginFill(0, 0.05);
            this.bg.graphics.drawRect(0, 0, param6, 1);
            this.bg.graphics.beginFill(8177663, 0.7);
            this.bg.graphics.drawRect(0, 0, 1, 39);
            this.bg.graphics.beginFill(8177663, 0.7);
            this.bg.graphics.drawRect((param6 - 1), 0, 1, 39);
            this.bg.alpha = 0;
            addChild(this.bg);
            this.thumbholder = new Sprite();
            this.thumbholder.x = 6;
            this.thumbholder.y = 4;
            addChild(this.thumbholder);
            this.thumb = new Bitmap();
            var _loc_8:int = 2;
            this.thumb.y = 2;
            this.thumb.x = _loc_8;
            this.thumbholder.addChild(this.thumb);
            switch(param3.type)
            {
                case LayerType.Bitmap:
                {
                    if (param5 > param4)
                    {
                        this.nw = Math.round(param4 * (28 / param5));
                        this.nh = 28;
                    }
                    else
                    {
                        this.nh = Math.round(param5 * (28 / param4));
                        this.nw = 28;
                    }
                    if (this.nw < 1)
                    {
                        this.nw = 1;
                    }
                    if (this.nh < 1)
                    {
                        this.nh = 1;
                    }
                    this.matrix = new Matrix();
                    this.matrix.scale(this.nw / param4, this.nh / param5);
                    this.thumbholder.graphics.beginFill(15658734);
                    this.thumbholder.graphics.drawRect(0, 0, this.nw + 4, this.nh + 4);
                    this.thumbholder.graphics.beginFill(0);
                    this.thumbholder.graphics.drawRect(1, 1, this.nw + 2, this.nh + 2);
                    this.thumbholder.graphics.beginBitmapFill(new Transparent());
                    this.thumbholder.graphics.drawRect(2, 2, this.nw, this.nh);
                    this.thumbholder.graphics.endFill();
                    break;
                }
                case LayerType.Text:
                {
                    this.thumbholder.graphics.beginFill(15658734);
                    this.thumbholder.graphics.drawRect(0, 0, 32, 32);
                    this.thumbholder.graphics.beginFill(0);
                    this.thumbholder.graphics.drawRect(1, 1, 30, 30);
                    this.thumbholder.graphics.beginFill(16777215);
                    this.thumbholder.graphics.drawRect(2, 2, 28, 28);
                    this.thumbholder.graphics.endFill();
                    this.thumb.x = 6;
                    this.thumb.y = 6;
                    this.thumb.bitmapData = new TypeIcon();
                    break;
                }
                default:
                {
                    break;
                }
            }
            this.ln = new Literal(param3.name);
            this.ln.addEventListener(MouseEvent.DOUBLE_CLICK, this.LayerNameDoubleClick, false, 1, true);
            this.ln.autoSize = TextFieldAutoSize.NONE;
            this.ln.doubleClickEnabled = true;
            this.ln.height = 18;
            this.ln.y = 5;
            addChild(this.ln);
            if (param2 != 0 || param1.canvas.document.transparent)
            {
                this.cb = new Checkbox();
                this.cb.x = param6 - 20;
                this.cb.y = 10;
                this.cb.addEventListener(ChangeEvent.CHANGE, this.VisibleClick, false, 1, true);
                this.cb.Check(param3.visible);
                addChild(this.cb);
            }
            else
            {
                this.locked = new IconButton(param1, "rm_double_click_to_unlock_layer", new LockedIcon());
                this.locked.doubleClickEnabled = true;
                this.locked.addEventListener(MouseEvent.DOUBLE_CLICK, this.LockedClick, false, 1, true);
                this.locked.x = param6 - 20;
                this.locked.y = 11;
                addChild(this.locked);
            }
            if (param3.filters != null && param3.filters.length > 0)
            {
                this.layerstyle = new IconButton(param1, "rm_layer_style", new FilterIcon());
                this.layerstyle.addEventListener(MouseEvent.CLICK, this.LayerStyleClick, false, 1, true);
                this.layerstyle.x = param6 - 40;
                this.layerstyle.y = 11;
                addChild(this.layerstyle);
            }
            if (param3.masking != null)
            {
                this.maskholder = new Sprite();
                this.maskholder.graphics.beginFill(15658734);
                this.maskholder.graphics.drawRect(0, 0, this.nw + 4, this.nh + 4);
                this.maskholder.graphics.beginFill(0);
                this.maskholder.graphics.drawRect(1, 1, this.nw + 2, this.nh + 2);
                this.maskholder.graphics.beginFill(16777215);
                this.maskholder.graphics.drawRect(2, 2, this.nw, this.nh);
                this.maskholder.graphics.endFill();
                this.maskholder.y = 4;
                addChild(this.maskholder);
                this.masking = new Bitmap();
                var _loc_8:int = 2;
                this.masking.y = 2;
                this.masking.x = _loc_8;
                this.maskholder.addChild(this.masking);
                this.maskholder.x = this.thumbholder.x + this.thumbholder.width + 5;
                this.ln.x = this.maskholder.x + this.maskholder.width + 5;
                this.maskholder.addEventListener(MouseEvent.MOUSE_DOWN, this.MarkerMouseDown, false, 1, true);
                this.thumbholder.addEventListener(MouseEvent.MOUSE_DOWN, this.MarkerMouseDown, false, 1, true);
                this.markselected = new Sprite();
                this.markselected.graphics.beginFill(16711680);
                this.markselected.graphics.drawRect(0, 0, this.nw + 2, 1);
                this.markselected.graphics.drawRect(0, (this.nh + 1), this.nw + 2, 1);
                this.markselected.graphics.drawRect(0, 1, 1, this.nh);
                this.markselected.graphics.drawRect((this.nw + 1), 1, 1, this.nh);
                this.markselected.graphics.endFill();
                this.markselected.graphics.beginFill(16711680);
                this.markselected.graphics.drawRect(this.nw - 5, this.nh - 5, 6, 6);
                this.markselected.graphics.endFill();
                this.markselected.graphics.beginFill(16777215);
                this.markselected.graphics.drawRect(this.nw - 4, this.nh - 4, 5, 1);
                this.markselected.graphics.drawRect(this.nw - 4, this.nh - 3, 1, 1);
                this.markselected.graphics.drawRect(this.nw - 4, this.nh - 2, 5, 1);
                this.markselected.graphics.drawRect(this.nw - 4, (this.nh - 1), 1, 1);
                this.markselected.graphics.drawRect(this.nw - 4, this.nh, 5, 1);
                this.markselected.graphics.endFill();
                this.markselected.visible = false;
                this.markselected.y = 5;
                this.markselected.x = param3.editMask ? ((this.maskholder.x + 1)) : ((this.thumbholder.x + 1));
                addChild(this.markselected);
            }
            else
            {
                this.ln.x = this.thumbholder.x + this.thumbholder.width + 5;
            }
            this.ln.width = 165 - this.ln.x - (this.layerstyle != null ? (20) : (0));
            this.UpdateBitmap();
            return;
        }// end function

        public function UpdateBitmap() : void
        {
            if (this.layer.type == LayerType.Bitmap && this.layer.bitmap != null)
            {
                if (this.thumb.bitmapData != null && this.thumb.width == this.nw && this.thumb.height == this.nh)
                {
                    this.thumb.bitmapData.fillRect(this.thumb.bitmapData.rect, 16777215);
                }
                else
                {
                    this.thumb.bitmapData = new BitmapData(this.nw, this.nh, true, 16777215);
                }
                this.matrix.tx = this.layer.x * this.matrix.a;
                this.matrix.ty = this.layer.y * this.matrix.d;
                this.thumb.bitmapData.draw(this.layer.bitmap, this.matrix, null, null, null, true);
            }
            if (this.layer.masking != null && this.layer.masking.bitmapData != null)
            {
                if (this.masking.bitmapData != null && this.masking.width == this.nw && this.masking.height == this.nh)
                {
                    this.masking.bitmapData.fillRect(this.masking.bitmapData.rect, 16777215);
                }
                else
                {
                    this.masking.bitmapData = new BitmapData(this.nw, this.nh, true, 16777215);
                }
                this.matrix.tx = this.layer.x * this.matrix.a;
                this.matrix.ty = this.layer.y * this.matrix.d;
                this.masking.bitmapData.draw(this.layer.masking.bitmapData, this.matrix, null, null, null, true);
            }
            return;
        }// end function

        public function LayerNameDoubleClick(event:Event) : void
        {
            this.ln.type = TextFieldType.INPUT;
            this.ln.selectable = true;
            this.ln.stage.focus = this.ln;
            this.ln.textColor = 0;
            this.ln.border = true;
            this.ln.borderColor = 4280427042;
            this.ln.background = true;
            this.ln.backgroundColor = 4293848814;
            this.ln.setSelection(0, this.ln.text.length);
            this.ln.addEventListener(FocusEvent.FOCUS_OUT, this.LayerNameFocusOut, false, 0, true);
            AppState.TextInteraction = true;
            return;
        }// end function

        public function LayerNameFocusOut(event:Event) : void
        {
            this.ln.removeEventListener(FocusEvent.FOCUS_OUT, this.LayerNameFocusOut, false);
            this.ln.type = TextFieldType.DYNAMIC;
            this.ln.selectable = false;
            this.ln.background = false;
            this.ln.border = false;
            if (this.bg.alpha == 1)
            {
                this.ln.textColor = 16777215;
            }
            else
            {
                this.ln.textColor = 3355443;
            }
            this.layer.name = this.ln.text;
            AppState.TextInteraction = false;
            return;
        }// end function

        public function Marking(width:Boolean) : void
        {
            if (width)
            {
                this.bg.alpha = 1;
                this.ln.textColor = 16777215;
                if (this.markselected != null)
                {
                    this.markselected.visible = true;
                }
            }
            else
            {
                this.bg.alpha = 0;
                this.ln.textColor = 3355443;
                if (this.markselected != null)
                {
                    this.markselected.visible = false;
                }
            }
            return;
        }// end function

        public function VisibleClick(event:Event) : void
        {
            this.layer.visible = this.cb.checked;
            this.ed.canvas.UpdateAfterEdit();
            this.ed.tool.Reset();
            return;
        }// end function

        public function LayerStyleClick(event:MouseEvent) : void
        {
            this.ed.cm.ShowLayerStyle();
            return;
        }// end function

        public function LockedClick(event:MouseEvent) : void
        {
            if (this.layer.name == U.RM("rm_background"))
            {
                this.layer.name = U.RM("rm_layer") + " 0";
            }
            this.ed.canvas.document.transparent = true;
            this.ed.layers.Update();
            this.ed.tool.Reset();
            return;
        }// end function

        private function MarkerMouseDown(event:MouseEvent) : void
        {
            switch(event.currentTarget)
            {
                case this.thumbholder:
                {
                    this.layer.editMask = false;
                    this.markselected.x = this.thumbholder.x + 1;
                    break;
                }
                case this.maskholder:
                {
                    this.layer.editMask = true;
                    this.markselected.x = this.maskholder.x + 1;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

    }
}
